//添加多个参数注意使用[]
layui.use(['form', 'jquery', 'laydate','upload'], function () {

    var form = layui.form,
        $ = layui.jquery,
        upload = layui.upload;

    //获取当前等用户
    let result = loginUserAjax();
    findById(result.data);
    //显示日期
    showTime('#birthday');
    /**
     * 图片上传
     */
    upload.render({
        elem: '#avatarImg',//绑定元素
        url: URL + '/upload/img', //请求接口
        done: function (reg) {
            //上传成功回调
            if (reg.code === 0) {
                $('#priview').attr('src', reg.data);
                $('#avatar').val(reg.data);
                layer.msg("上传成功！", {icon: 1, time: 1000, shade: 0.4})
            }
        },
        error: function () {
            //请求异常回调
        }
        , size: 1024 * 5 //最大5M
    });



    /**
     * 查询单个
     * @param id
     */
    function findById(id) {

        let result = commonAjax('/user/findById',{id:id},'get');
        setData(result.data);
    }

    /**
     * 赋值
     */
    function setData(data) {
        $('#id').val(data.id);
        $('#username').val(data.username);
        $('#studentId').val(data.studentId);
        $('#degree').val(data.degree);
        $('input[name=dataEnable][value=' + data.dataEnable + ']').next().click();
        //layUI设置单选框点击事件，需要获取它的兄弟元素，触发它的点击事件，显示样式
        $("input[name='gender'][value='" + data.gender + "']").next().click();
        $('#phone').val(data.phone);
        $('#email').val(data.email);
        $('#birthday').val(data.birthday);
        $('#team').val(data.team);
        $('#hometown').val(data.hometown);
        $('#address').val(data.address);
        $('#introduce').val(data.introduce);
        $('#sequence').val(data.sequence);
        //显示图片
        $('#priview').attr('src',data.avatar);
        $('#avatar').val(data.avatar);
        //下拉框赋值需要重新加载form.render
        form.render();
    }

    /**
     * 保存  //监听提交
     */
    form.on('submit(formDemo)', function (data) {

        let formData = JSON.stringify(data.field);
        let result = commonAjax('/user/update',formData,'post');
        if (result.count === 1) {
            layerMsgSaveSuccess();
        } else {
            layerMsgSaveFail();
        }
        return false;
    });

    /**
     * 关闭窗口
     */
    $('#close').on('click',function () {
        parent.layer.closeAll();
    })
});